<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/12/18
 * Time: 11:00
 */

namespace app\admin\controller;

use think\Db;

class Mycar extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            // 用户名/手机
            if(isset($keyword)&&$keyword!="")
            {
                $map['user_username|user_phone'] = ['like',"%" . $keyword . "%"];
            }
            // 车架号
            if(isset($mc_chejia)&&$mc_chejia!="")
            {
                $map['mc_chejia'] = ['like',"%" . $mc_chejia . "%"];
            }
            // 发动机号
            if(isset($mc_engine)&&$mc_engine!="")
            {
                $map['mc_engine'] = ['like',"%" . $mc_engine . "%"];
            }
            // 上报状态
            if(isset($mc_status)&&$mc_status!="")
            {
                $map['mc_status'] = $mc_status;
            }
            
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('my_car')
                ->join('up_u_user', 'mc_user_id = user_id', 'left')
                ->where($map)
                ->count();// 获取总条数
            $lists = Db::name('my_car')
                ->join('up_u_user', 'mc_user_id = user_id', 'left')
                ->where($map)
                ->field('mc_id, mc_paizhao, mc_chejia, mc_engine, mc_status, mc_time, user_username, user_phone')
                ->page($NowPage,$limits)
                ->order('mc_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 处理
     * @return \think\response\Json
     */
    public function done(){
        $mc_id = input('id');
        $res = Db::table('up_my_car')->where('mc_id', $mc_id)->setField('mc_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}